package com.group3.service.impl;

import com.group3.dao.*;
import com.group3.domain.*;
import com.group3.exception.BusinessException;
import com.group3.exception.ExceptionEnum;
import com.group3.pojo.receive.StudentReceive;
import com.group3.service.StudentInfoService;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * InfoServiceImpl
 *
 * @author HY
 * 2021/06/10 16:58
 **/


@Service
public class StudentStudentInfoServiceImpl implements StudentInfoService {

    @Resource
    private StudentInfoMapper studentInfoMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private GroupStudentMapper groupStudentMapper;
    @Resource
    private StudentGroupMapper studentGroupMapper;
    @Resource
    private LessonScoresMapper lessonScoresMapper;

    @Override
    public Result updateStudentInfo(String studentId, StudentReceive studentReceive) {
        Student student = new Student();
        student.setStudentId(studentId);

        int rs = studentMapper.updateByPrimaryKeySelective(student);

        return rs > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");
    }

    @Override
    public Result deleteStudent(List<String> studentIds) {

        //将学生从班级中移出，将该学生的班级id设为null
        for (String id : studentIds) {
            Student student = new Student();
            student.setStudentId(id);
            student.setClassId(null);
        }

        return ResultUtil.success("删除成功");
    }


    /**
     * 学生自己查询自己的信息（登录成功直接显示）
     */
    @Override
    public Result getInfoStudent(String studentId) {

        StudentInfoExample studentInfoExample = new StudentInfoExample();
        StudentInfoExample.Criteria criteria = studentInfoExample.createCriteria();
        criteria.andStudentIdEqualTo(studentId);//根据学生id获取个人信息
        /**
         * 装数据库自己的学生信息
         */
        List<StudentInfo> student = studentInfoMapper.selectByExample(studentInfoExample);
        /**
         * 获取小组id
         */
        StudentGroupExample studentGroupExample = new StudentGroupExample();
        studentGroupExample.createCriteria().andStudentIdEqualTo(studentId);
        List<StudentGroupKey> studentGroups = studentGroupMapper.selectByExample(studentGroupExample);
        List<Integer> studentGroupId = new ArrayList<>();
        for (StudentGroupKey studentGroupKey : studentGroups){
            studentGroupId.add(studentGroupKey.getGroupId());
        }
        /**
         * 获取小组成员信息
         */
        GroupStudentExample groupStudentExample = new GroupStudentExample();
        groupStudentExample.createCriteria().andGroupIdIn(studentGroupId);
        List<GroupStudent> groupStudents = groupStudentMapper.selectByExample(groupStudentExample);

        Map<String,Object> map = new HashMap<>();
        map.put("student",student);
        map.put("groupStudents",groupStudents);

        return ResultUtil.success(map);
    }

    /**
     * 查看自己的项目得分
     * @param studentId
     * @return
     */
    @Override
    public Result getProject(String studentId) {

        LessonScoresExample lessonScoresExample = new LessonScoresExample();
        lessonScoresExample.createCriteria().andStudentIdEqualTo(studentId);
        List<LessonScores> lessonScores = lessonScoresMapper.selectByExample(lessonScoresExample);

        return ResultUtil.success(lessonScores);
    }

    @Override
    public Result updateEmail(String studentId,String email) {
        Student student = new Student();
        student.setStudentId(studentId);
        student.setEmail(email);
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        int rs = studentMapper.updateemailByPrimaryKey(studentId,email);
        return rs > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");

    }

    @Override
    public Result updatePhone(String studentId, String tel) {
        Student student = new Student();
        student.setStudentId(studentId);
        student.setEmail(tel);
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        int rs = studentMapper.updatetelByPrimaryKey(studentId,tel);
        return rs > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");
    }
}
